package com.cloudpivot.receivales;

import com.alibaba.fastjson.JSONObject;
import com.cloudpivot.receivales.contract.domain.crmcontract.CrmContract;
import com.cloudpivot.receivales.contract.service.ContractAffairsService;
import com.cloudpivot.receivales.contract.service.http.HttpAPIServiceForCrm;
import com.cloudpivot.receivales.ding.scenegroup.domain.CoolAppActionMsgDTO;
import com.cloudpivot.receivales.ding.scenegroup.domain.CoolAppDTO;
import com.cloudpivot.receivales.ding.scenegroup.service.CoolAppService;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.Map;

/**
 * coolapp推送测试
 */
@SpringBootTest
class CloudpivotApplicationTest7 {

//    @Value("${myprops.crmurl}")
//    private String CRMAPI="http://172.17.100.193:5555/crm/api/data/v8.2/";

    @Value("${myprops.crmurl}")
    private String CRMAPI;

    @Autowired
    private HttpAPIServiceForCrm httpAPIServiceForCrm;

    @Autowired
    private ObjectMapper objectMapper;

    @Autowired
    private ContractAffairsService contractAffairsService;

    @Test
    public void testCrmPush() {

        try {
            CrmContract reqCrmContract = new CrmContract();
            String _str="";
            //循环增加字符
            for (int i = 0; i < 70000; i++) {
                _str+="2";
            }
            reqCrmContract.setNew_group_json(_str);
            reqCrmContract.setNew_name("测试字段长度用");

            //执行推送
            String reqUrl=String.format("%s%s", CRMAPI, "new_main_contracts");
            String ret=httpAPIServiceForCrm.doPostCrm(reqUrl, objectMapper.writeValueAsString(reqCrmContract));
            //获取创建合同的guid
            Map<String, Object> retMap = objectMapper.readValue(ret, Map.class);
            String retStr = retMap.get("new_main_contractid").toString();
            System.out.println("推送成功:"+retStr);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    @Test
    public void testCrmPush2() {
        try {
            JSONObject reqCrmContract = new JSONObject();
            String _str="";
            //循环增加字符
            for (int i = 0; i < 50000; i++) {
                _str+="4";
            }
            reqCrmContract.put("new_group_json",_str);
//            reqCrmContract.setNew_name("测试字段长度用");
            //执行推送
            String reqUrl=String.format("%s%s", CRMAPI, "new_main_contracts(c370c6b8-4223-f011-8ad8-80e25c682137)");
            String ret=httpAPIServiceForCrm.doUpdateCrm(reqUrl, JSONObject.toJSONString(reqCrmContract));
            //获取创建合同的guid
//            Map<String, Object> retMap = objectMapper.readValue(ret, Map.class);
//            String retStr = retMap.get("new_main_contractid").toString();
//            System.out.println("推送成功:"+retStr);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Test
    public void testCrmPush3(){
        contractAffairsService.pushContractAppendToCrm("b7c56c4d5dd448be9cc3727eb811dd2d");
    }

}
